package net.mingsoft.config;

import org.apache.shiro.mgt.SecurityManager;
import org.springframework.beans.factory.annotation.*;
import org.springframework.aop.framework.autoproxy.*;
import org.springframework.context.annotation.*;
import org.apache.shiro.spring.security.interceptor.*;
import org.apache.shiro.spring.web.*;
import net.mingsoft.basic.filter.*;
import javax.servlet.*;
import java.util.*;
import org.apache.shiro.web.session.mgt.*;
import org.apache.shiro.realm.*;
import org.apache.shiro.mgt.*;
import org.apache.shiro.session.mgt.*;
import org.apache.shiro.web.servlet.*;
import org.apache.shiro.web.mgt.*;
import org.apache.shiro.session.mgt.eis.*;
import net.mingsoft.basic.realm.*;
import net.mingsoft.basic.strategy.*;

@Configuration
public class ShiroConfig
{
    @Autowired(required = false)
    MSProperties msProperties;
    
    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        final DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }
    
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(final SecurityManager securityManager) {
        final AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }
    
    @Bean
    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(@Autowired(required = false) final DefaultWebSecurityManager securityManager) {
        final AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager((SecurityManager)securityManager);
        return advisor;
    }
    
    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        final DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setProxyTargetClass(true);
        return autoProxyCreator;
    }
    
    @Bean
    public ShiroFilterFactoryBean shirFilter(final SecurityManager securityManager) {
        final ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl(MSProperties.manager.path + "/login.do");
        shiroFilterFactoryBean.setUnauthorizedUrl(MSProperties.manager.path + "/404.do");
        final Map<String, Filter> filters = (Map<String, Filter>)shiroFilterFactoryBean.getFilters();
        filters.put("authc", (Filter)new ShiroLoginFilter());
        final Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/html/**", "anon");
        filterChainDefinitionMap.put(MSProperties.manager.path + "/login.do", "anon");
        filterChainDefinitionMap.put(MSProperties.manager.path + "/checkLogin.do", "anon");
        filterChainDefinitionMap.put(MSProperties.manager.path + "/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap((Map)filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }
    
    @Bean
    public SecurityManager securityManager(final DefaultWebSessionManager sessionManager) {
        final DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm((Realm)this.customRealm());
        securityManager.setRememberMeManager((RememberMeManager)this.rememberMeManager());
        securityManager.setSessionManager((SessionManager)sessionManager);
        return (SecurityManager)securityManager;
    }
    
    @Bean
    public DefaultWebSessionManager defaultWebSessionManager() {
        final DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionDAO((SessionDAO)this.getMemorySessionDAO());
        sessionManager.setSessionIdCookie((Cookie)this.getSimpleCookie());
        sessionManager.setSessionIdUrlRewritingEnabled(false);
        return sessionManager;
    }
    
    public SimpleCookie rememberMeCookie() {
        final SimpleCookie cookie = new SimpleCookie("rememberMe");
        cookie.setMaxAge(86400);
        return cookie;
    }
    
    public CookieRememberMeManager rememberMeManager() {
        final CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCookie((Cookie)this.rememberMeCookie());
        final String shiroKey = this.msProperties.getShiroKey();
        cookieRememberMeManager.setCipherKey(shiroKey.getBytes());
        return cookieRememberMeManager;
    }
    
    @Bean
    public MemorySessionDAO getMemorySessionDAO() {
        return new MemorySessionDAO();
    }
    
    @Bean
    public SimpleCookie getSimpleCookie() {
        final SimpleCookie simpleCookie = new SimpleCookie();
        simpleCookie.setName(this.msProperties.getCookieName());
        return simpleCookie;
    }
    
    @Bean
    public ManagerAuthRealm customRealm() {
        return new ManagerAuthRealm();
    }
    
    @Bean
    public IModelStrategy modelStrategy() {
        return new ManagerModelStrategy();
    }
    
    @Bean
    public ILoginStrategy loginStrategy() {
        return new ManagerLoginStrategy();
    }
}
